﻿using Domain;
using Domain.Entity;
using Domain.Repository;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EFCore.Repositorys
{
    public class SGDRepository : ISGDRepository
    {
        private readonly MyDbContext myDbContext;

        public SGDRepository(MyDbContext myDbContext)
        {
            this.myDbContext = myDbContext;
        }
        public async Task<bool> AddSgdAsync(SGD sgd)
        {
            myDbContext.SGD.Add(sgd);
            return await myDbContext.SaveChangesAsync() > 0;
        }

       public async Task<List<SGD>> GetAllSgdSalaryGrantIdAsync(string SalaryGrantId)
        {
            return await myDbContext.SGD.Where(u => u.SalaryGrantId == SalaryGrantId).ToListAsync();
        }

       public async Task<bool> RemoveSGDAsync(int GrdId, double BounsSum, double SaleSum, double DeductSum, double SalaryPaidSum)
        {
            var hfToModify = await myDbContext.SGD.FirstOrDefaultAsync(sgd => sgd.GrdId == GrdId);
            if (hfToModify != null)
            {
                hfToModify.BounsSum = BounsSum;
                hfToModify.SaleSum = SaleSum;
                hfToModify.DeductSum = DeductSum;
                hfToModify.SalaryPaidSum = SalaryPaidSum;
                myDbContext.Attach(hfToModify);
                myDbContext.Entry(hfToModify).State = EntityState.Modified;
                await myDbContext.SaveChangesAsync();
                return true;
            }
            return false;
        }
    }
}
